package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 进行关联查询
 */
public class JDBCDemo05 {
    public static void main(String[] args) {
        /*
            查看王克晶带的班的信息, 列出老师名字,性别,班级名,楼层
         */
        try (
                Connection connection = DBUtil.getConnection()
                ){
            System.out.println("服务器连接成功");
            Statement statement = connection.createStatement();
            /*
                SELECT t.name,t.gender,t.salary,c.name,c,floor
                FROM teacher t,class c
                WHERE t.id=c.teacher_id AND t.name='王克晶';
             */
            String sql = "SELECT t.name,t.gender,t.salary,c.name,c.floor\n" +
                         "FROM teacher t,class c\n" +
                         "WHERE t.id=c.teacher_id AND t.name='王克晶';";
            ResultSet rs = statement.executeQuery(sql);
            System.out.println("开始查询");
            while (rs.next()){
                String tName = rs.getString("t.name");
                String tGender = rs.getString("t.gender");
                int tSalary = rs.getInt("t.salary");
                String cName = rs.getString("c.name");
                int cFloor = rs.getInt("c.floor");
                System.out.println(String.join(",",tName,tGender,tSalary+"",cName,cFloor+""));
            }
            System.out.println("查询完成");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
